﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SPCoder.Utils.Nodes
{
    /// <summary>
    /// Represents the List node in treeview when connecting through CSOM
    /// </summary>
    public class ListNode : BaseNode, LeafNode
    {
         public ListNode(List list)
        {
            list.Context.Load(list);
            //list.Context.ExecuteQuery();            
            base.Title = list.Title;
            base.SPObjectType = list.GetType().Name;
            base.Url = list.Title;
            base.IconPath = list.ImageUrl;
        }

        public override object GetRealSPObject()
        {
            object objWeb = base.ParentNode.SPObject;
            if (objWeb != null)
            {                
                if (objWeb is Web)
                {
                    List list = ((Web)objWeb).Lists.GetByTitle(this.Title);
                    list.Context.Load(list);
                    list.Context.ExecuteQuery();
                    return list;
                }
            }

            return null;
        }
    }
}
